import { defineStore } from "pinia";
import { MenuState } from "../interface";
import piniaPersistConfig from "@/config/piniaPersist";
import router from "@/routers/router";
import { RouteRecordRaw } from "vue-router";
import { handleRouter } from "@/utils/util";

// MenuStore
export const MenuStore = defineStore({
	id: "MenuState",
	state: (): MenuState => ({
		// menu collapse
		isCollapse: false,
		// menu List
		menuList: []
	}),
	getters: {},
	actions: {
		async setCollapse() {
			this.isCollapse = !this.isCollapse;
		},
		async setMenuList(menuList: Menu.MenuOptions[]) {
			this.menuList = menuList;

			// 使用动态路由插入路由数据
			if (!router.hasRoute('system_menu')) {
				const routerList = handleRouter(menuList);
				routerList.forEach((item: RouteRecordRaw) => {
					router.addRoute(item);
				})
			}
		}
	},
	persist: piniaPersistConfig("MenuState")
});
